package com.example.front.main.shop;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.front.MyApplication;
import com.example.front.R;
import com.example.front.WelcomeActivity;
import com.example.front.main.home.ScenarioActivity;
import com.example.front.sign.LoginActivity;
import com.example.front.utils.MyAdapter;
import com.example.front.utils.ShopAdapter;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ProductActivity extends AppCompatActivity {
    private String URL;
    private MyApplication myApplication;
    private TextView product_number_textView;
    private ListView product_listView;
    private ShopAdapter shopAdapter = null;
    private List<SelectProduct> productList = null;
    private int count = 0;
    static Activity the;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        the=this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product);
        myApplication = (MyApplication)getApplication();
        URL = myApplication.getURL();
        product_number_textView = findViewById(R.id.product_number_textView);
        product_listView = findViewById(R.id.product_listView);
        product_number_textView.setText("已选择" + count + "件商品");
        OkHttpUtils.get()
                .url(URL + "/product/getProductList")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        Toast.makeText(ProductActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response) {
                        String json = response;
                        Log.i("success", "获取成功！");
                        productList = new ArrayList<SelectProduct>();
                        try {
                            JSONArray jsonArray = new JSONArray(json);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                                SelectProduct product = new SelectProduct();
                                product.setId(jsonObject.getInt("id"));
                                product.setName(jsonObject.getString("name"));
                                product.setPrice(jsonObject.getDouble("price"));
                                product.setUnit(jsonObject.getString("unit"));
                                product.setType(jsonObject.getString("type"));
                                product.setPhoto(jsonObject.getString("photo"));
                                product.setNumber(0);
                                productList.add(product);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        shopAdapter = new ShopAdapter(productList, ProductActivity.this);
                        product_listView.setAdapter(shopAdapter);
                    }
                });
    }

    public void onClick(View view){
        Object tag = view.getTag();
        switch (view.getId()){
            case R.id.add_button:
                if(tag != null && tag instanceof Integer){
                    int position = (Integer)tag;
                    int number = productList.get(position).getNumber();
                    number++;
                    if(number == 1){
                        count++;
                        product_number_textView.setText("已选择" + count + "件商品");
                    }
                    productList.get(position).setNumber(number);
                    shopAdapter.notifyDataSetChanged();
                }
                break;
            case R.id.subtract_button:
                if(tag != null && tag instanceof Integer){
                    int position = (Integer)tag;
                    int number = productList.get(position).getNumber();
                    if(number > 0) {
                        number--;
                        if (number == 0) {
                            count--;
                            product_number_textView.setText("已选择" + count + "件商品");
                        }
                        productList.get(position).setNumber(number);
                        shopAdapter.notifyDataSetChanged();
                    }
                }
                break;
            case R.id.settle_button:
                if(count == 0){
                    Toast.makeText(ProductActivity.this, "请先选择要购买的商品", Toast.LENGTH_SHORT).show();
                }
                else {
                    Intent intent = new Intent(ProductActivity.this, ShoppingCartActivity.class);
                    intent.putExtra("productList",(Serializable) productList);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
        }
    }
}
